package com.example.hf_v1.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.example.hf_v1.entity.Recharge;
import com.example.hf_v1.entity.User;
import com.example.hf_v1.entity.Vo.RechargeVo;
import com.example.hf_v1.mapper.RechargeMapper;
import com.example.hf_v1.mapper.UserMapper;
import com.example.hf_v1.service.IRechargeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 充值表
 服务实现类
 * </p>
 *
 * @author 浪花骑士
 * @since 2024-08-07
 */
@Service
public class RechargeServiceImpl extends ServiceImpl<RechargeMapper, Recharge> implements IRechargeService {

    @Resource
    private RechargeMapper rechargeMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public boolean updateRecharge(Recharge recharge) {
        return false;
    }

    @Override
    public List<RechargeVo> selectAllRecharge() {
        MPJLambdaWrapper<Recharge> wrapper = new MPJLambdaWrapper(Recharge.class);
        wrapper.selectAs(Recharge::getRechargeId, RechargeVo::getRechargeId)
                .selectAs(Recharge::getRechargeUserId,RechargeVo::getRechargeUserId)
                .leftJoin(User.class,User::getId,Recharge::getRechargeUserId)
                .selectAs(User::getUsername,RechargeVo::getUserName)
                .selectAs(User::getAccountBalance,RechargeVo::getAccountBalance)
                .selectAs(Recharge::getRechargeSaveMoney,RechargeVo::getRechargeSaveMoney)
                .selectAs(Recharge::getRechargeOverTime,RechargeVo::getRechargeOverTime)
                .selectAs(Recharge::getRechargeTinyTime,RechargeVo::getRechargeTinyTime)
                .selectAs(User::getUpdatedAt,RechargeVo::getUpdateTime)
                .list();
        return rechargeMapper.selectJoinList(RechargeVo.class,wrapper);
    }

    @Override
    public List<RechargeVo> selectAllRechargeById(Integer id) {
        MPJLambdaWrapper<Recharge> wrapper = new MPJLambdaWrapper(Recharge.class);
        wrapper.selectAs(Recharge::getRechargeId, RechargeVo::getRechargeId)
                .selectAs(Recharge::getRechargeUserId,RechargeVo::getRechargeUserId)
                .leftJoin(User.class,User::getId,Recharge::getRechargeUserId)
                .selectAs(User::getUsername,RechargeVo::getUserName)
                .selectAs(User::getAccountBalance,RechargeVo::getAccountBalance)
                .selectAs(Recharge::getRechargeSaveMoney,RechargeVo::getRechargeSaveMoney)
                .selectAs(Recharge::getRechargeOverTime,RechargeVo::getRechargeOverTime)
                .selectAs(Recharge::getRechargeTinyTime,RechargeVo::getRechargeTinyTime)
                .selectAs(User::getUpdatedAt,RechargeVo::getUpdateTime)
                .eq(id!=null,Recharge::getRechargeId,id);
        return rechargeMapper.selectJoinList(RechargeVo.class,wrapper);
    }

    @Override
    public List<RechargeVo> selectAllRechargeByTime(String time) {
        MPJLambdaWrapper<Recharge> wrapper = new MPJLambdaWrapper(Recharge.class);
        wrapper.selectAs(Recharge::getRechargeId, RechargeVo::getRechargeId)
                .selectAs(Recharge::getRechargeUserId,RechargeVo::getRechargeUserId)
                .leftJoin(User.class,User::getId,Recharge::getRechargeUserId)
                .selectAs(User::getUsername,RechargeVo::getUserName)
                .selectAs(User::getAccountBalance,RechargeVo::getAccountBalance)
                .selectAs(Recharge::getRechargeSaveMoney,RechargeVo::getRechargeSaveMoney)
                .selectAs(Recharge::getRechargeOverTime,RechargeVo::getRechargeOverTime)
                .selectAs(Recharge::getRechargeTinyTime,RechargeVo::getRechargeTinyTime)
                .selectAs(User::getUpdatedAt,RechargeVo::getUpdateTime)
                .like(time!=null,Recharge::getRechargeTinyTime,"%" + time + "%")
                .list();
        return rechargeMapper.selectJoinList(RechargeVo.class,wrapper);
    }

    @Override
    public int saveAndUpdate(Recharge recharge) {
        //查询用户
        User user = userMapper.selectById(recharge.getRechargeUserId());
        if(user == null) {
            throw new RuntimeException("用户不存在");
        }
        //


        // 增加用户余额
        if( recharge.getRechargeSaveMoney() <= 0) {
            throw new RuntimeException("无效的充值金额");
        }else {
            // 增加加用户余额
            user.setAccountBalance(user.getAccountBalance()+ recharge.getRechargeSaveMoney());

            //将用户修改为vip用户,当日期等于用户VIP到期日时，将用户vip状态修改为“否”
            //获取当前系统时间,返回值类型是LocalDateTime
            LocalDate currentDateTime = LocalDate.now();
            //修改用户表更新日期
            user.setUpdatedAt(LocalDateTime.now());
            // 修改VIP到期日
            int number = (int)(user.getAccountBalance()/100);
            recharge.setRechargeOverTime(recharge.getRechargeOverTime().plusMonths(number));
            if(user.getAccountBalance() <= 0 || recharge.getRechargeOverTime().equals(currentDateTime)){
                user.setVip("否");
            } else{
                user.setVip("是");
            }
            // 保存用户信息
            userMapper.updateById(user);
        }
        // 保存充值记录
        return rechargeMapper.insert(recharge);
    }


}
